package cn.xyinping.pet.controller;

import cn.xyinping.basic.exception.MyException;
import cn.xyinping.basic.util.AjaxResult;
import cn.xyinping.basic.util.PageList;
import cn.xyinping.pet.domain.Pet;
import cn.xyinping.pet.query.PetQuery;
import cn.xyinping.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    /**
     * 宠物上下架页面展示
     *
     * @param query
     * @return
     */
    @PostMapping("/pagelist")
    public AjaxResult pagelist(@RequestBody PetQuery query) {
        try {
            PageList<Pet> list = petService.queryList(query);
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙");
        }
    }

    /**
     * 宠物上架
     *
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody List<Long> ids) {
        try {
            petService.publish(ids);
            return AjaxResult.me();
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙");
        }
    }

    /**
     * 宠物下架
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            petService.offsale(ids);
            return AjaxResult.me();
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙");
        }
    }

    /**
     * 上架后宠物列表展示
     * @return
     */
    @PostMapping("/pageListForOnsale")
    public AjaxResult pageListForOnsale(@RequestBody PetQuery query){
        try {
            // 查询上架后的宠物
            query.setState(Pet.STATE_ONLINE);
            PageList<Pet> pageList = petService.queryList(query);
            return AjaxResult.me().setResultObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙");
        }
    }

    /**
     * 上架宠物显示详情
     * @param id
     * @return
     */
    @GetMapping("/selectOnLineById/{id}")
    public AjaxResult selectOnLineById (@PathVariable("id") Long id){
        try {
            Pet pet = petService.selectOnLineById(id);
            return AjaxResult.me().setResultObj(pet);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙");
        }
    }

}
